package com.gm.commonlib.base.baseui.baseFragment;

import android.app.Activity;
import android.app.Application;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.gm.commonlib.application.CommonBaseApplication;
import com.gm.commonlib.base.baseView.IBaseView;
import com.gm.commonlib.module.evenbus.EventBusUtils;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.views.dialog.loadingDialog.ILoadingDialog;
import com.gm.commonlib.views.toast.IToast;
import com.youth.banner.util.LogUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class CommonAbstractBaseFragment extends Fragment implements IBaseView {

    protected Activity mActivity;
    protected Context context;

    protected CommonBaseApplication app;
    protected ILoadingDialog mILoadingDialog; //加载提示框

    protected boolean mIsShowToast=true;
    protected IToast mToast; //toast
    abstract ILoadingDialog createLoadingDialog();
    abstract IToast createToast();
    protected CommonBaseApplication getApplication() {
        return app;
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    protected void installAppContext(Application application) {
        app = (CommonBaseApplication) application;
    }

    protected void init(){
        mActivity=getActivity();
        installAppContext(getApplication());
        //加载框初始化
        mILoadingDialog=createLoadingDialog();
        mToast=createToast();
    }


    @Override
    public void onDestroy() {
        hidLoadingDialog();
        //todo 销毁时候，解除事件监听
        EventBusUtils.unregister(this);
        super.onDestroy();
    }


    public void showToast(String text) {
        if (mToast!=null && mIsShowToast){
            mToast.showToast(text);
        }
    }

    public Dialog showLoadingDialog() {
        return showLoadingDialog(null);
    }

    public Dialog showLoadingDialog(String msg) {
        if (mILoadingDialog!=null){
            return mILoadingDialog.show(context,msg,false);
        }
        return null;
    }

    public Dialog showLoadingDialog(String msg,boolean cancelable) {
        if (mILoadingDialog!=null){
            return mILoadingDialog.show(context,msg,cancelable);
        }
        return null;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        hidLoadingDialog();
    }

    public void hidLoadingDialog() {
       if (mILoadingDialog!=null){
           mILoadingDialog.hid();
       }
    }

    @Override
    public void onStart() {
        super.onStart();
        //todo 每个继承这个基类的类 启动就注册上evenbus事件监听
        EventBusUtils.register(this);
    }



    /**
     * 接收到分发的事件
     *
     * @param event 事件
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveEvent(EventMessage event) {
        if (event==null)return;
        LogUtils.d("onReceiveEvent: "+event.toString());
    }



}
